//
//  DianTaiInfoTableViewController.m
//  ProJectB_1
//
//  Created by lanou3g on 16/8/15.
//  Copyright © 2016年 yangshukai. All rights reserved.
//

#import "DianTaiInfoTableViewController.h"

@interface DianTaiInfoTableViewController ()

@property (nonatomic,strong)NSMutableArray *dataInfoArray;// 数据源

@property (nonatomic,strong)NSMutableArray *playListArray;// 播放列表
@property (strong, nonatomic) IBOutlet UITableView *InfoTable;

@end

@implementation DianTaiInfoTableViewController

- (NSMutableArray *)dataInfoArray{
    if (!_dataInfoArray) {
        _dataInfoArray = [NSMutableArray arrayWithCapacity:0];
    }
    return _dataInfoArray;
}
- (NSMutableArray *)playListArray{
    if (!_playListArray) {
        _playListArray = [NSMutableArray arrayWithCapacity:0];
    }
    return _playListArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"DianTaiInfoTableViewCell" bundle:nil] forCellReuseIdentifier:@"DianTaiInfoTableViewCell"];
    self.InfoTable.separatorStyle = UITableViewCellSeparatorStyleNone; // 去掉分割线
    [self getData];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:(UIBarButtonItemStylePlain) target:self action:@selector(back:)];
}
- (void)back:(UIBarButtonItem *)sender{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)getData{
    [NetWorkRequestManager requestWithType:POST urlString:DianTaiInFoLIST_URL parDic:@{@"radioid":_radioid, @"auth":@"Wc06FCrkoq1DCMVzGMTikDJxQ8bm3Mrm2NpT9qWjwzcWP23tBKQx1c4P"} finish:^(NSData *data) {
        if (data == nil) {
            return ;
        }
        dispatch_async(dispatch_get_global_queue(0, 0), ^{
            NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingAllowFragments) error:nil];
            NSArray *listArr = dict[@"data"][@"list"];
            for (NSDictionary *dic in listArr) {
                DianTaiInfoModel *listModel = [[DianTaiInfoModel alloc]init];
                [listModel setValuesForKeysWithDictionary:dic];
                [self.dataInfoArray addObject:listModel];
                // 添加播放列表数据源
                [self.playListArray addObject:dic[@"musicUrl"]];
                NSLog(@"---------------%@",dic[@"musicUrl"]);
                NSLog(@"-=-=-=-=-=-=-=-=%ld",self.playListArray.count);
            }
            NSDictionary *headDic = dict[@"data"][@"radioInfo"];
            HeadListModel *headModel = [[HeadListModel alloc]init];
            [headModel setValuesForKeysWithDictionary:headDic];
            // 设置头视图图片
            dispatch_async(dispatch_get_main_queue(), ^{
                NSArray *views = [[NSBundle mainBundle] loadNibNamed:@"HeadView" owner:nil options:nil];
                HeadView *head = views.lastObject;
                [head setData:headModel];
                head.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 290 + head.LabelHeight.constant);
                _InfoTable.tableHeaderView = head;
                [_InfoTable reloadData];
            });
        });
    } error:^(NSError *error) {
        NSLog(@"error : %@",error);
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataInfoArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    DianTaiInfoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DianTaiInfoTableViewCell" forIndexPath:indexPath];
    DianTaiInfoModel *model = _dataInfoArray[indexPath.row];
    
    [cell setDataWithModel:model];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    DianTaiInfoModel *model = [_dataInfoArray objectAtIndex:indexPath.row];
    NSLog(@"model = %@", model);
    //创建播放详情
    PlayViewController *radioPlayVC = [[PlayViewController alloc] initWithNibName:@"PlayViewController" bundle:nil];
    radioPlayVC.selectPlay = indexPath.row;
    radioPlayVC.dianTaiInfoModel = model;
    radioPlayVC.playListArray = self.playListArray;
    NSLog(@"========================%ld",_playListArray.count);
    radioPlayVC.detailListArray = self.dataInfoArray;
    [self.navigationController pushViewController:radioPlayVC animated:YES];
}

- (CGFloat)stringHeight:(NSString *)aString{
    CGRect temp = [aString boundingRectWithSize:CGSizeMake(355, 10000) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:15]} context:nil];
    return temp.size.height;
}

@end
